<?php

namespace App\Http\Controllers\api\sick;

use App\Http\Controllers\Controller;
use App\Logic\IdCardImageVerify;
use App\Logic\sick\CreateSick;
use App\Models\Sick;
use App\Server\ApiService;
use App\Server\QiniuService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class SickController extends Controller
{
    /**
     * Name: 上传身份证照片
     * User: 王梓墨
     * Date: 2023/6/27
     * Time:13:50
     * @param Request $request
     */
    public function createIdCardImage(Request $request)
    {
        $file = $request->file('id_card_image');
        $data = IdCardImageVerify::run($file);
        $data = explode(',',$data);
//        dd($data);  //找到上传成功的标识
        if (isset($data[32]) && ($data[32] == '"image_status":"normal"'))
        {
            $img=Storage::disk('public')->put('shixunhealthcare/id_card_image', $file);
            $image=QiniuService::qiniu("storage/".$img);
            return ApiService::success('上传身份证照片成功',$image);
        } else {
            return ApiService::fail('照片不是身份证');
        }
//        dd($file);

    }

    /**
     * Name: 提交上传身份证
     * User: 王梓墨
     * Date: 2023/6/27
     * Time:15:09
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function createIdCard(Request $request)
    {
        $params = $request->post();
//        dd($params);

        $params['idCardImage'] = $request->post('idCardImage');

        $data = Sick::where('name',$params['name'])->update(
            [
                'id_card_image' => $params['idCardImage']
            ]
        );

        if ($data)
        {
            return ApiService::success('添加本人身份证照片成功',$data);
        } else {
            return ApiService::fail('添加本人身份证照片失败');
        }

    }

    /**
     * Name: 上传本人照片
     * User: 王梓墨
     * Date: 2023/6/27
     * Time:13:49
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|string
     */
    public function createSickImg(Request $request)
    {
        $file=$request->file('sick_img');

        $img=Storage::disk('public')->put('shixunhealthcare/sick', $file);
        $image=QiniuService::qiniu("storage/".$img);

        return $image;
    }

    /**
     * Name: 添加患者
     * User: 王梓墨
     * Date: 2023/6/27
     * Time:13:49
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function createSick(Request $request)
    {
        $params = $request->post();
        $userInfo = DB::table('admin')->where('id',1)->first();
        $params['user_id'] = $userInfo->id;
        $params['phone'] = $userInfo->phone;
        $params['patient_id_card'] = $userInfo->patient_id_card;
        $params['status'] = 0;

        $params['sick_img'] = $request->post('sick_img');

        return CreateSick::createSick($params);
    }
}
